﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Infrastructure;
using Picking.Common;
using PickingBusiness;
using Picking_Winform.Util;
//using System.Threading;
using System.Configuration;

namespace Picking_Winform
{
    public partial class ManualOperationView : FormBase
    {
        private int currentWorkerCount = 0;

        private WorkersAndLightsSetupModel modelWorkersAndLights = new WorkersAndLightsSetupModel();
        private SebangoAndDNoSetupModel modelSebangoDNo = new SebangoAndDNoSetupModel();
        private LightAndDNoSetupModel modelLightDNo = new LightAndDNoSetupModel();

        private InterfaceUpdateHandler LightTurnOffHandle;

        private List<string> lightNosWorker1 = new List<string>();
        private List<string> lightNosWorker2 = new List<string>();
        private List<string> lightNosWorker3 = new List<string>();

        /// <summary>
        /// Constructor
        /// </summary>
        public ManualOperationView()
        {
            InitializeComponent();
            this.Controls.Add(base.txtTitle);
        }

        /// <summary>
        /// Get Form Width
        /// </summary>
        /// <returns></returns>
        protected override int GetFormWidth()
        {
            return 820;
        }

        /// <summary>
        /// Set Form Title
        /// </summary>
        private void SetFormTitle()
        {
            this.Text = txtTitle.Text = "手动作业";
        }

        /// <summary>
        /// Form Shown
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManualOperationView_Shown(object sender, EventArgs e)
        {
            this.SetFormTitle();
            this.TurnOnLEDGreen();
        }

        /// <summary>
        /// Form Load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManualOperationView_Load(object sender, EventArgs e)
        {
            this.GetWorkersAndLightsSetup();
            this.InitOperationArea();

            this.InitSebangoAndDNoSetup();
            this.InitLightAndDNoSetup();

            this.LightTurnOffHandle
                = new InterfaceUpdateHandler(HandleTurnedOffLightNo);

            this.InitAIPlug();

#if DEBUG
            this.cmbSerialPortTest.Visible = true;
            this.btnTest.Visible = true;
            this.btnLoad.Visible = true;
#endif
        }

        /// <summary>
        /// Get Current WorkersAndLightsSetup Data From Database
        /// </summary>
        private void GetWorkersAndLightsSetup()
        {
            this.modelWorkersAndLights = PickingBusiness.Configuration.GetWorkersAndLightsSetup();

            this.currentWorkerCount = modelWorkersAndLights.WorkersAndLightsSetupTable.Rows.Count;
        }

        /// <summary>
        /// Init Operation Area
        /// </summary>
        private void InitOperationArea()
        {
            if (this.currentWorkerCount <= 0)
            {
                this.tlpMain.Visible = false;
            }
            else if (this.currentWorkerCount <= 1)
            {
                this.tlpOperationArea2.Visible = false;
                this.tlpOperationArea3.Visible = false;

                this.tlpMain.ColumnStyles.Clear();
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 50F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 50F));

                float lblFontSize = 30F;
                float txtFontSize = 30F;
                float btnFontSize = 30F;
                // Set Font
                this.lblOperation1.Font = new System.Drawing.Font("SimSun", lblFontSize, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.lblSebango1.Font = new System.Drawing.Font("SimSun", lblFontSize, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.txtSebango1.Font = new System.Drawing.Font("SimSun", txtFontSize, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.btnOK1.Font = new System.Drawing.Font("SimSun", btnFontSize, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            }
            else if (this.currentWorkerCount <= 2)
            {
                this.tlpOperationArea3.Visible = false;

                this.tlpMain.ColumnStyles.Clear();
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 10F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 10F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 10F));

                float lblFontSize = 25F;
                float txtFontSize = 25F;
                float btnFontSize = 25F;
                // Set Font
                this.lblOperation1.Font = new System.Drawing.Font("SimSun", lblFontSize, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.lblSebango1.Font = new System.Drawing.Font("SimSun", lblFontSize, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.txtSebango1.Font = new System.Drawing.Font("SimSun", txtFontSize, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.btnOK1.Font = new System.Drawing.Font("SimSun", btnFontSize, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

                // Set Font
                this.lblOperation2.Font = new System.Drawing.Font("SimSun", lblFontSize, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.lblSebango2.Font = new System.Drawing.Font("SimSun", lblFontSize, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.txtSebango2.Font = new System.Drawing.Font("SimSun", txtFontSize, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.btnOK2.Font = new System.Drawing.Font("SimSun", btnFontSize, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            }
            else if (this.currentWorkerCount <= 3)
            {
                float lblFontSize = 20F;
                float txtFontSize = 20F;
                float btnFontSize = 20F;
                // Set Font
                this.lblOperation1.Font = new System.Drawing.Font("SimSun", lblFontSize, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.lblSebango1.Font = new System.Drawing.Font("SimSun", lblFontSize, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.txtSebango1.Font = new System.Drawing.Font("SimSun", txtFontSize, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.btnOK1.Font = new System.Drawing.Font("SimSun", btnFontSize, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

                // Set Font
                this.lblOperation2.Font = new System.Drawing.Font("SimSun", lblFontSize, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.lblSebango2.Font = new System.Drawing.Font("SimSun", lblFontSize, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.txtSebango2.Font = new System.Drawing.Font("SimSun", txtFontSize, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.btnOK2.Font = new System.Drawing.Font("SimSun", btnFontSize, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

                // Set Font
                this.lblOperation3.Font = new System.Drawing.Font("SimSun", lblFontSize, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.lblSebango3.Font = new System.Drawing.Font("SimSun", lblFontSize, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.txtSebango3.Font = new System.Drawing.Font("SimSun", txtFontSize, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.btnOK3.Font = new System.Drawing.Font("SimSun", btnFontSize, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            }
        }

        /// <summary>
        /// Init Sebango And DNo
        /// Get Sebango And DNo Setup From Database
        /// </summary>
        private void InitSebangoAndDNoSetup()
        {
            this.modelSebangoDNo = PickingBusiness.Configuration.GetSebangoAndDNoSetup();
        }

        /// <summary>
        /// Init DNo
        /// Get Light And DNo Setup From Database
        /// </summary>
        private void InitLightAndDNoSetup()
        {
            this.modelLightDNo = PickingBusiness.Configuration.GetLightAndDNoSetup();
        }

        /// <summary>
        /// Init AIPlug
        /// </summary>
        private void InitAIPlug()
        {
            if (!string.IsNullOrWhiteSpace(ConfigInfo.AxAiPlugIpAddress))
            {
                axAiPlug.ConnectSocket(ConfigInfo.AxAiPlugIpAddress, ConfigInfo.AxAiPlugPort);

                axAiPlug.StartPolling(); //watch the data arrival

                axAiPlug.BinaryMode = true; //set to true

                axAiPlug.CommandTimeout = 5000; //set to 5000

                this.axAiPlug.DataArrival
                    += new AxAiPlugControl.@__AiPlug_DataArrivalEventHandler(this.axAiPlug_DataArrival);
            }
        }

        /// <summary>
        /// Display Light No
        /// </summary>
        /// <param name="ltvLight"></param>
        /// <param name="inputSebangoNo"></param>
        /// <param name="workerNumber"></param>
        private void DisplayLightNo(ListView ltvLight, string inputSebangoNo, int workerNumber)
        {
            ltvLight.Items.Clear();

            int sebango = 0;

            if (int.TryParse(inputSebangoNo.Trim(), out sebango))
            {
                foreach (DataRow row in this.modelSebangoDNo.SebangoAndDNoSetupTable.Rows)
                {
                    int sebagoDNo = 0;

                    if (int.TryParse(row["Sebango_ID"].ToString().Trim(), out sebagoDNo))
                    {
                        if (sebango == sebagoDNo)
                        {
                            string currentDNo = row["Design_No"].ToString().Trim();
                            foreach (DataRow rowLight in this.modelLightDNo.LightAndDNoSetupTable.Rows)
                            {
                                if (string.Equals(currentDNo, rowLight["Design_No"].ToString().Trim()))
                                {
                                    string lightNo = rowLight["Light_No"].ToString().Trim();

                                    int lightNumber = -1;

                                    int.TryParse(lightNo, out lightNumber);

                                    int startLightNo = this.GetLightStartNo(workerNumber);
                                    int endLightNo = this.GetLightEndNo(workerNumber);

                                    if ((lightNumber >= startLightNo)
                                        && (lightNumber <= endLightNo))
                                    {
                                        ltvLight.Items.Add(lightNumber.ToString("0000"));

                                        if (workerNumber == 1)
                                        {
                                            this.lightNosWorker1.Add(lightNumber.ToString("0000"));
                                        }
                                        else if (workerNumber == 2)
                                        {
                                            this.lightNosWorker2.Add(lightNumber.ToString("0000"));
                                        }
                                        else if (workerNumber == 3)
                                        {
                                            this.lightNosWorker3.Add(lightNumber.ToString("0000"));
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Get Light Start No
        /// </summary>
        /// <param name="workerNumber"></param>
        /// <returns></returns>
        private int GetLightStartNo(int workerNumber)
        {
            int startLightNo = 0;

            foreach (DataRow row in this.modelWorkersAndLights.WorkersAndLightsSetupTable.Rows)
            {
                if (string.Equals(row["Picking_Worker_No"].ToString().Trim(), workerNumber.ToString()))
                {
                    int.TryParse(row["Light_Start_No"].ToString().Trim(), out startLightNo);
                }
            }

            return startLightNo;
        }

        /// <summary>
        /// Get Light End No
        /// </summary>
        /// <param name="workerNumber"></param>
        /// <returns></returns>
        private int GetLightEndNo(int workerNumber)
        {
            int endLightNo = 0;

            foreach (DataRow row in this.modelWorkersAndLights.WorkersAndLightsSetupTable.Rows)
            {
                if (string.Equals(row["Picking_Worker_No"].ToString().Trim(), workerNumber.ToString()))
                {
                    int.TryParse(row["Light_End_No"].ToString().Trim(), out endLightNo);
                }
            }

            return endLightNo;
        }

        /// <summary>
        /// Turn On Light
        /// </summary>
        /// <param name="lightNos"></param>
        private void TurnOnLight(List<string> lightNos)
        {
            string lightNoToTurnOn = string.Empty;

            foreach (string item in lightNos)
            {
                lightNoToTurnOn += item + "88888";

                if (lightNoToTurnOn.Trim().Length >= (ConfigInfo.LightNumInOneCommand * 9))
                {
                    this.axAiPlug.SendCommand("P100" + lightNoToTurnOn.Trim());
                    lightNoToTurnOn = string.Empty;
                }
            }

            if (!string.IsNullOrEmpty(lightNoToTurnOn.Trim()))
            {
                this.axAiPlug.SendCommand("P100" + lightNoToTurnOn.Trim());
            }
        }

        /// <summary>
        /// Turn On LED Yellow
        /// </summary>
        private void TurnOnLEDYellow()
        {
            //this.axAiPlug.SendCommand("T" + ConfigInfo.CmdLEDYellow.Trim());
        }

        /// <summary>
        /// Turn On LED Green
        /// </summary>
        private void TurnOnLEDGreen()
        {
            this.axAiPlug.SendCommand("T" + ConfigInfo.CmdLEDGreen.Trim());
        }

        /// <summary>
        /// Turn On LED Red
        /// </summary>
        private void TurnOnLEDRed()
        {
            this.axAiPlug.SendCommand("T" + ConfigInfo.CmdLEDRed.Trim());
        }

        /// <summary>
        /// Turn On LED Beep
        /// </summary>
        private void TurnOnLEDBeep()
        {
            this.axAiPlug.SendCommand("T" + ConfigInfo.CmdLEDBeep.Trim());
        }

        /// <summary>
        /// Turn On LED Red And Beep
        /// </summary>
        private void TurnOnLEDRedAndBeep()
        {
            this.TurnOnLEDRed();
            //this.TurnOnLEDBeep();
        }

        /// <summary>
        /// Get Light No
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        private string GetLightNo(string data)
        {
            string lightNo = string.Empty;
            lightNo = data.Substring(1, 4);

            return lightNo;
        }

        /// <summary>
        /// Get Light Status
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        private string GetLightStatus(string data)
        {
            string status = string.Empty;
            if (data.Length >= 7)
            {
                status = data.Substring(5, 2);
            }
            else if (data.Length >= 6)
            {
                status = data.Substring(5, data.Length - 5);
            }

            return status;
        }

        /// <summary>
        /// Handle Turned Off Light No
        /// </summary>
        /// <param name="data"></param>
        /// <param name="port"></param>
        private void HandleTurnedOffLightNo(string data, string port)
        {
            if (this.currentWorkerCount <= 0)
            {
                return;
            }

            string lightNo = this.GetLightNo(data);
            string lightStatus = this.GetLightStatus(data);

            if (this.currentWorkerCount <= 1)
            {
                this.ClearLightNo(this.ltvLight1, lightNo, lightStatus, this.lightNosWorker1, 0);
            }
            else if (this.currentWorkerCount <= 2)
            {
                this.ClearLightNo(this.ltvLight1, lightNo, lightStatus, this.lightNosWorker1, 0);
                this.ClearLightNo(this.ltvLight2, lightNo, lightStatus, this.lightNosWorker2, 1);
            }
            else
            {
                this.ClearLightNo(this.ltvLight1, lightNo, lightStatus, this.lightNosWorker1, 0);
                this.ClearLightNo(this.ltvLight2, lightNo, lightStatus, this.lightNosWorker2, 1);
                this.ClearLightNo(this.ltvLight3, lightNo, lightStatus, this.lightNosWorker3, 2);
            }
        }

        /// <summary>
        /// Clear Light No
        /// </summary>
        /// <param name="ltvLight"></param>
        /// <param name="lightNo"></param>
        /// <param name="lightStatus"></param>
        private void ClearLightNo(ListView ltvLight, string lightNo, string lightStatus,
                                  List<string> lightNos, int line)
        {
            for (int i = ltvLight.Items.Count - 1; i >= 0; i--)
            {
                if (string.Equals(ltvLight.Items[i].Text.Substring(0, lightNo.Length), lightNo))
                {
                    if (string.Equals(lightStatus, "00"))
                    {
                        ////ltvLight.Items[i].Remove();
                        ////ltvLight.Items[i].Text = lightNo;
                        ltvLight.Items[i].ForeColor = System.Drawing.Color.Gray;
                    }
                    else
                    {
                        ////ltvLight.Items[i].Text = lightNo + "异常";
                        ltvLight.Items[i].ForeColor = System.Drawing.Color.Red;
                        this.TurnOnLEDRedAndBeep();
                    }
                }
            }

            if (this.IsAllLightTurnedOff(ltvLight))
            {
                if (line == 0 && ltvLight.Items.Count > 0) TurnOnBeep1();
                if (line == 1 && ltvLight.Items.Count > 0) TurnOnBeep2();
                if (line == 2 && ltvLight.Items.Count > 0) TurnOnBeep3();
                ltvLight.Items.Clear();
            }
        }

        private void TurnOnBeep3()
        {
            this.axAiPlug.SendCommand("T80051");
            thread = new System.Threading.Thread(p =>
            {
                object[] obj = p as object[];
                System.Threading.Thread.Sleep(int.Parse(obj[2].ToString()));
                AxAiPlugControl.AxAiPlug plugin2 = obj[0] as AxAiPlugControl.AxAiPlug;
                string cmd = obj[1].ToString();
                this.axAiPlug.SendCommand(cmd);
            }, 0);
            thread.Start(new object[] { this.axAiPlug, "T80050", ConfigurationManager.AppSettings["BeepTime"] });
        }

        private void TurnOnBeep2()
        {
            this.axAiPlug.SendCommand("T80041");
            thread = new System.Threading.Thread(p =>
            {
                object[] obj = p as object[];
                System.Threading.Thread.Sleep(int.Parse(obj[2].ToString()));
                AxAiPlugControl.AxAiPlug plugin2 = obj[0] as AxAiPlugControl.AxAiPlug;
                string cmd = obj[1].ToString();
                this.axAiPlug.SendCommand(cmd);
            }, 0);
            thread.Start(new object[] { this.axAiPlug, "T80040", ConfigurationManager.AppSettings["BeepTime"] });
        }

        private System.Threading.Thread thread;
        private void TurnOnBeep1()
        {
            this.axAiPlug.SendCommand("T80021");
            thread = new System.Threading.Thread(p =>
            {
                object[] obj = p as object[];
                System.Threading.Thread.Sleep(int.Parse(obj[2].ToString()));
                AxAiPlugControl.AxAiPlug plugin2 = obj[0] as AxAiPlugControl.AxAiPlug;
                string cmd = obj[1].ToString();
                this.axAiPlug.SendCommand(cmd);
            }, 0);
            thread.Start(new object[] { this.axAiPlug, "T80020", ConfigurationManager.AppSettings["BeepTime"] });
        }


        /// <summary>
        /// Is All Light Turned Off
        /// </summary>
        /// <param name="ltvLight"></param>
        /// <returns></returns>
        private bool IsAllLightTurnedOff(ListView ltvLight)
        {
            bool isAllLightTurnedOff = true;

            for (int i = ltvLight.Items.Count - 1; i >= 0; i--)
            {
                if (ltvLight.Items[i].ForeColor != System.Drawing.Color.Gray)
                {
                    isAllLightTurnedOff = false;
                    break;
                }
            }

            return isAllLightTurnedOff;
        }

        /// <summary>
        /// Do Validation
        /// </summary>
        /// <param name="inputSebangoNo"></param>
        /// <param name="workerNumber"></param>
        /// <returns></returns>
        private bool DoValidation(string inputSebangoNo, int workerNumber)
        {
            bool isSebangoExisting = false;
            bool isSebangoHasLightNo = false;
            bool isSebangoHasLightNoUnderWorker = false;
            int sebango = 0;

            if (string.IsNullOrEmpty(inputSebangoNo.Trim()))
            {
                MessageBox.Show("背番号不能为空!",
                                "验证",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Warning);
                return false;
            }

            if (int.TryParse(inputSebangoNo.Trim(), out sebango))
            {
                foreach (DataRow row in this.modelSebangoDNo.SebangoAndMachineSetupTable.Rows)
                {
                    int sebagoDNo = 0;

                    if (int.TryParse(row["Sebango_ID"].ToString().Trim(), out sebagoDNo))
                    {
                        if (sebango == sebagoDNo)
                        {
                            isSebangoExisting = true;
                            break;
                        }
                    }
                }

                if (!isSebangoExisting)
                {
                    MessageBox.Show("背番号不存在，请重新输入!",
                                    "验证",
                                     MessageBoxButtons.OK,
                                     MessageBoxIcon.Warning);
                    return false;
                }

                foreach (DataRow row in this.modelSebangoDNo.SebangoAndDNoSetupTable.Rows)
                {
                    int sebagoDNo = 0;

                    if (int.TryParse(row["Sebango_ID"].ToString().Trim(), out sebagoDNo))
                    {
                        if (sebango == sebagoDNo)
                        {
                            isSebangoHasLightNo = true;

                            string currentDNo = row["Design_No"].ToString().Trim();
                            foreach (DataRow rowLight in this.modelLightDNo.LightAndDNoSetupTable.Rows)
                            {
                                if (string.Equals(currentDNo, rowLight["Design_No"].ToString().Trim()))
                                {
                                    string lightNo = rowLight["Light_No"].ToString().Trim();

                                    int lightNumber = -1;

                                    int.TryParse(lightNo, out lightNumber);

                                    int startLightNo = this.GetLightStartNo(workerNumber);
                                    int endLightNo = this.GetLightEndNo(workerNumber);

                                    if ((lightNumber >= startLightNo)
                                        && (lightNumber <= endLightNo))
                                    {
                                        isSebangoHasLightNoUnderWorker = true;
                                    }
                                }
                            }
                        }
                    }
                }

                if (!isSebangoHasLightNo)
                {
                    MessageBox.Show("该背番号尚没有关联的棚灯号，请重新输入!",
                                    "验证",
                                     MessageBoxButtons.OK,
                                     MessageBoxIcon.Warning);
                    return false;
                }

                if (!isSebangoHasLightNoUnderWorker)
                {
                    MessageBox.Show("该背番号下的棚灯号不属于本工作人员的操作范围，请重新输入!",
                                    "验证",
                                     MessageBoxButtons.OK,
                                     MessageBoxIcon.Warning);
                    return false;
                }
            }
            else
            {
                MessageBox.Show("背番号必须是个整型数值，请重新输入!",
                                "验证",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Warning);

                return false;
            }

            return true;
        }

        /// <summary>
        /// btnOK1 Click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK1_Click(object sender, EventArgs e)
        {
            if (!this.DoValidation(this.txtSebango1.Text.Trim(), 1))
            {
                return;
            }

            this.lightNosWorker1.Clear();
            this.DisplayLightNo(this.ltvLight1, this.txtSebango1.Text.Trim(), 1);
            this.TurnOnLight(this.lightNosWorker1);

            if (this.ltvLight1.Items.Count > 0)
            {
                this.btnOK1.Enabled = false;
                this.btnReturn.Enabled = false;
            }
        }

        /// <summary>
        /// btnOK2 Click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK2_Click(object sender, EventArgs e)
        {
            if (!this.DoValidation(this.txtSebango2.Text.Trim(), 2))
            {
                return;
            }

            this.lightNosWorker2.Clear();
            this.DisplayLightNo(this.ltvLight2, this.txtSebango2.Text.Trim(), 2);
            this.TurnOnLight(this.lightNosWorker2);

            if (this.ltvLight2.Items.Count > 0)
            {
                this.btnOK2.Enabled = false;
                this.btnReturn.Enabled = false;
            }
        }

        /// <summary>
        /// btnOK3 Click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK3_Click(object sender, EventArgs e)
        {
            if (!this.DoValidation(this.txtSebango3.Text.Trim(), 3))
            {
                return;
            }

            this.lightNosWorker3.Clear();
            this.DisplayLightNo(this.ltvLight3, this.txtSebango3.Text.Trim(), 3);
            this.TurnOnLight(this.lightNosWorker3);

            if (this.ltvLight3.Items.Count > 0)
            {
                this.btnOK3.Enabled = false;
                this.btnReturn.Enabled = false;
            }
        }

        /// <summary>
        /// Return Event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnReturn_Click(object sender, EventArgs e)
        {
            this.CloseCurrentWindow();
        }



        /// <summary>
        /// Form Closing
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManualOperationView_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.axAiPlug.SendCommand("T80010");
            this.axAiPlug.SendCommand("T80020");
            this.axAiPlug.SendCommand("T80030");
            this.axAiPlug.SendCommand("T80040");
            this.axAiPlug.SendCommand("T80050");
            this.axAiPlug.StopPolling();
            this.axAiPlug.Dispose();
        }

        /// <summary>
        /// Form Resize
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManualOperationView_SizeChanged(object sender, EventArgs e)
        {
            this.tlpMain.Height = this.Height - 150;
        }

        /// <summary>
        /// axAiPlug Data Arrival
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void axAiPlug_DataArrival(object sender, AxAiPlugControl.__AiPlug_DataArrivalEvent e)
        {
            string data = e.data;
            //data=t000100xxxx
            //t:fixed, 0001:light no, 
            //00:normal(else:exception)

            data = data.Replace("\n", string.Empty);
            data = data.Replace("\r", string.Empty);

            if (data.Length < 5)
            {
                return;
            }

            if (LightTurnOffHandle != null)
            {
                this.Invoke(LightTurnOffHandle,
                            new string[] { data, string.Empty });
            }

            if (this.ltvLight1.Items.Count == 0)
            {
                this.btnOK1.Enabled = true;
            }

            if (this.ltvLight2.Items.Count == 0)
            {
                this.btnOK2.Enabled = true;
            }

            if (this.ltvLight3.Items.Count == 0)
            {
                this.btnOK3.Enabled = true;
            }

            if ((this.ltvLight1.Items.Count == 0)
                && (this.ltvLight2.Items.Count == 0)
                && (this.ltvLight3.Items.Count == 0))
            {
                this.btnReturn.Enabled = true;
            }
        }

        /// <summary>
        /// Load Data For Test
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLoad_Click(object sender, EventArgs e)
        {
            this.cmbSerialPortTest.Items.Clear();
            foreach (string item in this.lightNosWorker1)
            {
                this.cmbSerialPortTest.Items.Add("t" + item + "00");
            }

            foreach (string item in this.lightNosWorker2)
            {
                this.cmbSerialPortTest.Items.Add("t" + item + "00");
            }

            foreach (string item in this.lightNosWorker3)
            {
                this.cmbSerialPortTest.Items.Add("t" + item + "00");
            }
        }

        /// <summary>
        /// Test
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnTest_Click(object sender, EventArgs e)
        {
            AxAiPlugControl.__AiPlug_DataArrivalEvent e2
                = new AxAiPlugControl.__AiPlug_DataArrivalEvent(this.cmbSerialPortTest.Text);

            this.axAiPlug_DataArrival(null, e2);
        }
    }
}